package dao.impl;

import dao.BaseDao;
import dao.PowerBatteryDao;
import entity.PowerBattery;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PowerBatteryDaoImpl extends BaseDao implements PowerBatteryDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    @Override
    public List<PowerBattery> powerList() {
        conn = super.getConnection();
        List<PowerBattery> list = new ArrayList<>();
        PowerBattery powerBattery = null;
        try{
            String sql = "select * from powerbattery order by time asc";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                powerBattery = new PowerBattery();
                powerBattery.setId(rs.getInt("id"));
                powerBattery.setCarName(rs.getString("carName"));
                powerBattery.setCityName(rs.getString("cityName"));
                powerBattery.setPressureValue(rs.getInt("pressureValue"));
                powerBattery.setTime(rs.getString("time"));
                powerBattery.setUserName(rs.getString("userName"));
                list.add(powerBattery);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            super.getClose(conn,ps,rs);
        }
        return list;
    }

    @Override
    public PowerBattery powerSelect(int id) {
        conn = super.getConnection();
        PowerBattery powerBattery = new PowerBattery();
        try{
            String sql = "select * from powerbattery where id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if(rs.next()){
                powerBattery.setCarName(rs.getString("carName"));
                powerBattery.setCityName(rs.getString("cityName"));
                powerBattery.setPressureValue(rs.getInt("pressureValue"));
                powerBattery.setTime(rs.getString("time"));
                powerBattery.setUserName(rs.getString("userName"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            super.getClose(conn,ps,rs);
        }
        return powerBattery;
    }

    @Override
    public int powerUpdate(PowerBattery powerBattery) {
        conn = super.getConnection();
        int count = 0;
        try{
            String sql = "update powerbattery set carName=?,cityName=?,userName=?,pressureValue=?,time=? where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,powerBattery.getCarName());
            ps.setString(2,powerBattery.getCityName());
            ps.setString(3,powerBattery.getUserName());
            ps.setInt(4,powerBattery.getPressureValue());
            ps.setString(5,powerBattery.getTime());
            ps.setInt(6,powerBattery.getId());
            if(ps.executeUpdate()>0){
                count = 1;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            super.getClose(conn,ps,rs);
        }
        return count;
    }

    @Override
    public int powerDelete(int id) {
        conn = super.getConnection();
        int count = 0;
        try{
            String sql = "delete from powerbattery where id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            if(ps.executeUpdate()>0){
                count = 1;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            super.getClose(conn,ps,rs);
        }
        return count;
    }
}
